package com.dhcc.bpm.common.utils;


import com.dhcc.bpm.common.json.JSONObject;
import com.dhcc.bpm.common.redisMessage.RedisMessageSender;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.core.tool.utils.SpringUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


/**
 * @ClassName MessageUtils
 * @Description 发送消息工具类
 * @Author daina
 * @Date 2020/3/19 10:01
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 **/
@Component
public class MessageUtils {
    private static final Logger log = LoggerFactory.getLogger(MessageUtils.class);

    private static RedisMessageSender redisMessageSender;

    /** 消息Key */
    private static String KEY;

    @Value("${bpm.message.key}")
    public void setKey(String key) {
        KEY = key;
    }

    private static RedisMessageSender redisMessageSender(){
        if (redisMessageSender == null){
            redisMessageSender = SpringUtil.getBean("redisMessageSender");
        }
        return redisMessageSender;
    }

    /**
     * @Description 推送消息 --redis 消息推送
     * @Author daina
     * @Date 14:48 2020/3/19
     * @param title
     * @param id
     * @return boolean
     **/
    public boolean sendMessage(String title, String id) {
        JSONObject info = new JSONObject();
        info.set("key", KEY);
        info.set("title", title);
        info.set("id", id);
        boolean bool = false;
        try {
            bool = redisMessageSender().produceMessage(ContentUtils.SEND_MESSAGE, info);
        } catch (Exception e) {
            log.error("推送消息异常：{}", e);
            return false;
        }

        return bool;
    }

}
